package by.dkusch.aircompany.webapp.page.flight.panel;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.apache.wicket.Page;
import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.event.IEvent;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.PageCreator;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.ResourceModel;

import by.dkusch.aircompany.model.tables.Airport;
import by.dkusch.aircompany.model.tables.Route;
import by.dkusch.aircompany.model.tables.Route_;
import by.dkusch.aircompany.services.AirportService;
import by.dkusch.aircompany.services.RouteService;
import by.dkusch.aircompany.webapp.page.base.enums.ActionType;
import by.dkusch.aircompany.webapp.page.base.enums.DirectionType;
import by.dkusch.aircompany.webapp.page.base.events.SelectorDirectionEvent;
import by.dkusch.aircompany.webapp.page.base.validator.UniqueFieldValidator;
import by.dkusch.aircompany.webapp.page.regional.panel.AirportListPanel;

public class RouteEditPanel extends Panel {

	private static final long serialVersionUID = 1L;
	@Inject
	private RouteService routeService;
	@Inject
	private AirportService airportService;

	public RouteEditPanel(String id, final Route route, final PageCreator pageCreator, ActionType action) {
		super(id);

		Form<Route> form = new Form<Route>("form", new CompoundPropertyModel<Route>(route));

		final TextField<String> tfName = new TextField<String>("name");
		tfName.add(new PropertyValidator<String>()).setLabel(new ResourceModel("route.list.name"));
		form.add(tfName);

		final TextField<String> tfDuration = new TextField<String>("duration");
		tfDuration.add(new PropertyValidator<String>()).setLabel(new ResourceModel("route.list.duration"));
		form.add(tfDuration);

		final TextField<String> tfStart = new TextField<String>("startPoint.code") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onEvent(IEvent<?> event) {
				Object payload = event.getPayload();
				if (payload instanceof SelectorDirectionEvent<?>) {
					SelectorDirectionEvent<?> selectorEvent = (SelectorDirectionEvent<?>) payload;
					if (selectorEvent.getDirection().equals(DirectionType.DIRECTION_FROM)) {
						Airport airport = airportService.get((Long) selectorEvent.getEntityID());
						route.setStartPoint(airport);
						setDefaultModelObject(airport != null ? airport.getCode() : getString("air.error.detail.empty"));
						selectorEvent.getTarget().add(this);
					}

				}
			}

		};
		tfStart.add(new PropertyValidator<String>()).setLabel(new ResourceModel("route.list.airport.start")).setEnabled(false);
		form.add(tfStart);

		final TextField<String> tfEnd = new TextField<String>("endPoint.code") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onEvent(IEvent<?> event) {
				Object payload = event.getPayload();
				if (payload instanceof SelectorDirectionEvent<?>) {
					SelectorDirectionEvent<?> selectorEvent = (SelectorDirectionEvent<?>) payload;
					if (selectorEvent.getDirection().equals(DirectionType.DIRECTION_TO)) {
						Airport airport = airportService.get((Long) selectorEvent.getEntityID());
						route.setEndPoint(airport);
						setDefaultModelObject(airport != null ? airport.getCode() : getString("air.error.detail.empty"));
						selectorEvent.getTarget().add(this);
					}

				}
			}
		};
		tfEnd.add(new PropertyValidator<String>()).setLabel(new ResourceModel("route.list.airport.end")).setEnabled(false);
		form.add(tfEnd);

		final TextField<String> tfDescription = new TextField<String>("description");
		tfDescription.add(new PropertyValidator<String>()).setLabel(new ResourceModel("route.list.description"));
		form.add(tfDescription);

		final CheckBox chBArc = new CheckBox("arcFl");
		chBArc.add(new PropertyValidator<Boolean>()).setLabel(new ResourceModel("route.list.arcfl"));
		form.add(chBArc);

		form.add(new Label("correctDt"));

		final SubmitLink submitLink = new SubmitLink("sumbit-link") {

			private static final long serialVersionUID = 1L;
			private List<String> errorMessage = new ArrayList<String>();

			@Override
			public void onSubmit() {
				super.onSubmit();
				errorMessage.clear();
				if (route.getStartPoint() == null) {
					errorMessage.add(getString("StartPointEmptyMessage"));
				}
				if (route.getEndPoint() == null) {
					errorMessage.add(getString("EndPointEmptyMessage"));
				}
				if (errorMessage.size() > 0) {
					onError();
					return;
				}

				routeService.saveOrUpdate(route);

				Page page = pageCreator.createPage();
				page.info(getString("column.common.action.saved"));
				setResponsePage(page);
			}

			@Override
			public void onError() {

				for (String msg : errorMessage) {
					error(msg);
				}
			}

		};
		submitLink.setOutputMarkupId(true);
		form.add(submitLink);

		final TextField<String> tfCode = new TextField<String>("code");
		tfCode.add(new PropertyValidator<String>()).setLabel(new ResourceModel("route.list.code"));
		tfCode.add(new UniqueFieldValidator<Long, Route>(Route_.code, routeService, route)).setLabel(new ResourceModel("route.list.code"));
		form.add(tfCode);

		add(new Link<Void>("back-link") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				setResponsePage(pageCreator.createPage());
			}

		});
		AirportListPanel airportListPanel = new AirportListPanel("airport-panel", 10, true);
		form.add(airportListPanel.setVisible(action.equals(ActionType.EDIT)));

		add(form);
		form.setEnabled(action.equals(ActionType.EDIT));

	}

}
